package com.carrie.mdg.myviewtext.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.carrie.mdg.myviewtext.R;
import com.carrie.mdg.myviewtext.aidl.Book;

import org.litepal.crud.DataSupport;
import org.litepal.tablemanager.Connector;

import java.util.List;

import androidLib.activity.BaseActivity;

public class LitePalTextActivity extends BaseActivity {

    private Button createDatabase;
    private Button addData;
    private Button updateData;
    private Button deleteData;
    private Button queryData;

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, LitePalTextActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initVariables() {

    }

    @Override
    protected void initViews(Bundle savedInstanceState) {
        setContentView(R.layout.activity_lite_pal_text);
        createDatabase = (Button) findViewById(R.id.button_create_database_litepal);
        addData = (Button) findViewById(R.id.button_add_data_litepal);
        updateData = (Button) findViewById(R.id.button_update_data_litepal);
        deleteData = (Button) findViewById(R.id.button_delete_data_litepal);
        queryData = (Button) findViewById(R.id.button_query_data_litepal);
    }

    @Override
    protected void initEvent() {
        createDatabase.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Connector.getDatabase();
            }
        });
        addData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Book book = new Book();
                book.setName("The Da Vinci Code");
                book.setAuthor("Dan Brown");
                book.setPages(454);
                book.setPrice(16.96);
                book.setPress("UnKnown");
                book.save();
            }
        });
        updateData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Book book = new Book();
//                book.setName("The Lost Symbol");
//                book.setAuthor("Dan Brown");
//                book.setPages(514);
//                book.setPrice(19.95);
//                book.setPress("Unknown");
//                book.save();
//                book.setPrice(10.95);
//                book.save();

//                Book book = new Book();
//                book.setPrice(14.95);
//                book.setPress("Anchor");
//                book.updateAll("name = ? and author = ?","The Lost Symbol","Dan Brown");

                //将数据更新成初始默认值,updateAll()中没有指定条件，所以更新所有数据的pages一栏
                Book book = new Book();
                book.setToDefault("pages");
                book.updateAll();
            }
        });
        deleteData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DataSupport.deleteAll(Book.class, "price < ?", "15");
            }
        });
        queryData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //p252讲查询语句的例子
                List<Book> books = DataSupport.findAll(Book.class);

                Book firstBook = DataSupport.findFirst(Book.class);

                Book lastBook = DataSupport.findLast(Book.class);

            }
        });
    }

    @Override
    protected void loadData() {

    }
}
